//<!-- License -->
package eu.datafusion.log4soa.model.referencedata;

import static javax.persistence.EnumType.ORDINAL;
import static javax.persistence.FetchType.EAGER;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Enumerated;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MappedSuperclass;

import eu.datafusion.log4soa.model.DataProtection;
import eu.datafusion.log4soa.model.referencedata.security.Role;

import javax.persistence.JoinColumn;

/**
 * BaseReferenceData represents base class for Application, Service and Operation classes
 * @author Ivo.Mitov@datafusion.eu
 *
 */
@MappedSuperclass
public abstract class BaseReferenceData extends NamedEntity implements Serializable   {

	/**
	 * generated
	 */
	private static final long serialVersionUID = 3415905533144781747L;
	
	/**
	 * DataProtection associated with Application, Service or Operation
	 */
	@Enumerated(ORDINAL)
	@Column(name = "DATAPROTECT")
	private DataProtection dataProtection ;
	
	/**
	 * roles associated with ReferenceDataEntity
	 */
	@ManyToMany(fetch = EAGER)
	@JoinTable(name = "REFERENCE_ROLES", joinColumns = @JoinColumn(name = "REF_ID", referencedColumnName = "ID", unique = false), inverseJoinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ID"))
	private Set<Role> roles;
	
	/**
	 * @param name required immutable property <tt>name</tt> must be unique per concrete class instances
	 * @param dataProtection
	 * @param description optional property 
	 */
	protected BaseReferenceData(String name,DataProtection dataProtection, String description) {
		super(name,description);
		this.setDataProtection(dataProtection);
	}
	
	/**
	 * @param name required immutable property <tt>name</tt> must be unique per concrete class instances
	 * @param dataProtection
	 */
	protected BaseReferenceData(String name, DataProtection dataProtection) {
		this(name,dataProtection,null);
	}

	/**
	 * @param name required immutable property <tt>name</tt> must be unique per concrete class instances
	 * @param description optional property
	 */
	protected BaseReferenceData(String name, String description) {
		this(name,DataProtection.UNPROTECTED,description);
	}
	
	/**
	 * @param name required immutable property <tt>name</tt>, must be unique per concrete class instances
	 */
	protected BaseReferenceData(String name) {
		this(name,DataProtection.UNPROTECTED,null);
	}

	/**
	 * default constructor required by JPA
	 */
	protected BaseReferenceData(){
		super();
	}
	
	/** 
	 * Getter of the property <tt>dataProtection</tt>
	 * @return  Returns the dataProtection.
	 */
	public DataProtection getDataProtection() {
		return dataProtection;
	}

	/** 
	 * Setter of the property <tt>dataProtection</tt>
	 * @param dataProtection.
	 */
	public void setDataProtection(DataProtection dataProtection) {
		this.dataProtection = dataProtection;
	}
	
	/**
	 * Getter of the property <tt>roles</tt> init roles if null
	 * @return roles
	 */
	public Set<Role> getRoles() {
		if(roles==null){
			roles = new HashSet<Role>();
		}
		return roles;
	}
	
	/** 
	 * Setter of the property <tt>roles</tt>
	 * @param roles
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
	/**
	 * add role to property <tt>roles</tt>
	 * @param role
	 */
	public void addRole(Role role){
		getRoles().add(role);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((dataProtection == null) ? 0 : dataProtection.hashCode());
		result = prime * result + ((roles == null) ? 0 : roles.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		BaseReferenceData other = (BaseReferenceData) obj;
		if (dataProtection != other.dataProtection)
			return false;
		if (roles == null) {
			if (other.roles != null)
				return false;
		} else if (!roles.equals(other.roles))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return super.toString() + ", dataProtection=" + dataProtection + ", roles=" + roles;
	}


	

	

}
